package com.uni.web.util;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.uni.common.util.SystemConfig;

public class WxPayUtil {

	public static Map<String, String> pay(String orderNo,String title,Integer totalFee,String ip,String opendId,int type) {
		WXPay wxPay = new WXPay(new WxConfig());
		Map<String, String> reqData = new HashMap<String, String>();
//		reqData.put("sub_appid", WechatUtil.appid);
//		reqData.put("sub_mch_id", WechatUtil.mchId);
//		reqData.put("appid", WechatUtil.fwsappid);
//        reqData.put("mch_id", WechatUtil.fwsmchId);
		JSONObject jo = new JSONObject();
		jo.put("type", type);
		reqData.put("attach", jo.toJSONString());
        reqData.put("appid", WechatUtil.appid);
        reqData.put("mch_id", WechatUtil.mchId);
		reqData.put("nonce_str", WXPayUtil.generateNonceStr());
		reqData.put("body", title);
		reqData.put("out_trade_no", orderNo);
		reqData.put("total_fee", totalFee.toString());
		reqData.put("spbill_create_ip", ip);
		reqData.put("notify_url", SystemConfig.baseUrl + WechatUtil.notifyUrl);
		reqData.put("trade_type", "JSAPI");
//		reqData.put("sub_openid", opendId);
		reqData.put("openid", opendId);
		
		
		try {
			String sign = WXPayUtil.generateSignature(reqData, WechatUtil.key);
			reqData.put("sign", sign);
			Map<String, String> wxResult = wxPay.unifiedOrder(reqData);
			if("SUCCESS".equals(wxResult.get("result_code"))) {
				return wxResult;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	
	/**
	 * 退款
	 * @param map
	 * @return
	 */
	public static Map<String, String> wxRefund(Map<String, String> map) {
		WXPay wxPay = new WXPay(new WxConfig());
		Map<String, String> reqData = new HashMap<>();//构建调用refund方法所需要的参数
		
		/** appid 公众号ID*/
//		reqData.put("appid", WechatUtil.fwsappid);
//		/** mch_id 商户号ID*/
//		reqData.put("mch_id", WechatUtil.fwsmchId);
//		
//		reqData.put("sub_appid", WechatUtil.appid);
//		/** mch_id 商户号ID*/
//		reqData.put("sub_mch_id", WechatUtil.mchId);
		
		reqData.put("appid", WechatUtil.appid);
//		/** mch_id 商户号ID*/
		reqData.put("mch_id", WechatUtil.mchId);
		/** nonce_str 随机字符串*/
		reqData.put("nonce_str", WXPayUtil.generateNonceStr());
		/** sign 签名*/
		/** sign_type 签名类型*/
		
		/**transaction_id 微信订单号  或  out_trade_no商户订单号*/
		reqData.put("out_trade_no", map.get("outTradeNo"));
		/** 商户退款单号*/
		reqData.put("out_refund_no", map.get("outRefundNo"));
		/** 订单金额*/
		reqData.put("total_fee", map.get("totalFee"));
		/** 退款金额 */
		reqData.put("refund_fee", map.get("refundFee"));
		
		reqData.put("refund_desc", map.get("refundDesc"));
		
		reqData.put("notify_url", SystemConfig.baseUrl + WechatUtil.refundNotifyUrl);
		
		reqData.put("refund_account", "REFUND_SOURCE_RECHARGE_FUNDS");
//		System.out.println(reqData);
		
		Map<String, String> refund = new HashMap<>();
//		if("SUCCESS".equals(refund.get("result_code"))) {
		try {
			reqData.put("sign", WXPayUtil.generateSignature(reqData, WechatUtil.key));
			refund = wxPay.refund(reqData);//调用WXPay的refund方法，返回退款是否成功
//			System.out.println(refund);
			return refund;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return refund;
	}
}
